package com.cstor.liang.service;

import com.google.common.collect.Lists;
import com.cstor.liang.mapper.RegionMapper;
import com.cstor.liang.pojo.entity.Region;
import com.cstor.liang.pojo.other.RegionJo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional(readOnly = true)
public class RegionService extends BaseService<Region>{

    public static int PROVINCE = 1;
    public static int CITY = 2;
    public static int COUNTY = 3;
    public static int STREET = 4;


    @Autowired
    private RegionMapper regionMapper;

    /**
     * 列出全部省份
     *
     * @return
     */
    public List<RegionJo> getAllProvince() {
        return regionMapper.listProvince();
    }

    /**
     * 获取某个省的全部城市
     *
     * @param regionId 省份id
     * @return
     */
    @Cacheable("getAllCity")
    public List<RegionJo> getAllCity(Long regionId) {
        List<RegionJo> result = Lists.newArrayList();
        //获取区域码
        String regionCode = regionMapper.getRegionCode(regionId);
        if (regionCode != null) {
            result = regionMapper.listCity(regionCode.substring(0, 2) + "%");
        }

        return result;
    }

    /**
     * 获取某个城市的所有县
     *
     * @param regionId 城市id
     * @return
     */
    @Cacheable("getAllCounty")
    public List<RegionJo> getAllCounty(Long regionId) {
        List<RegionJo> result = Lists.newArrayList();
        //获取区域码
        String regionCode = regionMapper.getRegionCode(regionId);
        if (regionCode != null) {
            result = regionMapper.listCounty(regionCode.substring(0, 4) + "%");
        }

        return result;
    }

    /**
     * 获取某个县的所有街道
     *
     * @param regionId 县id
     * @return
     */
    @Cacheable("getAllStreet")
    public List<RegionJo> getAllStreet(Long regionId) {
        List<RegionJo> result = Lists.newArrayList();
        //获取区域码
        String regionCode = regionMapper.getRegionCode(regionId);
        if (regionCode != null) {
            result = regionMapper.listStreet(regionCode.substring(0, 6) + "%");
        }

        return result;
    }

    /**
     * 根据城市id，获取所有的县和街道
     *
     * @param cityId 城市id
     * @return
     */
    public List<Region> getCountyAndStreet(Long cityId) {

        //获取区域码
        String regionCode = regionMapper.getRegionCode(cityId);

        return regionMapper.listCountyAndStreet(regionCode.substring(0, 4) + "%");
    }
}
